package Offer.offer2019;

import java.util.ArrayList;

/**
 * 顺时针打印矩阵
 * @author: leiwenkang02@meituan.com
 * @date: 2019/4/30
 * @time: 10:31
 */
public class Offer20 {
    public static void main(String[] args) {
        int[][] matrix = {{1, 2, 3, 4, 5}, {6, 7, 8, 9 ,10}, {11, 12, 13, 14, 15}, {16, 17, 18, 19, 20}};
        ArrayList<Integer> result = new Offer20().printMatrix2(matrix);
        System.out.println(result);
    }

    public ArrayList<Integer> printMatrix2(int [][] matrix) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return null;
        }

        ArrayList<Integer> result = new ArrayList();
        int rows = matrix.length;
        int columns = matrix[0].length;

        int start = 0;
        while (2 * start < rows && 2 * start < columns) {
            printCircle2(matrix, start, rows, columns, result);
            start++;
        }
        return result;
    }

    private void printCircle2(int[][] matrix, int start,
                             int rows, int columns, ArrayList<Integer> result) {
        int endRow = rows - start - 1;
        int endColumn = columns - start - 1;
        //从左到右打印一行  行数固定为start,列数逐渐增至endColumn
        for (int i = start; i <= endColumn; i++) {
            result.add(matrix[start][i]);
        }

        //从上到下打印一列，列数固定为endColumn，行数从 start + 1逐渐增至endRow
        if (endRow >= start + 1) {
            for (int i = start + 1; i <= endRow; i++) {
                result.add(matrix[i][endColumn]);
            }

            //从右到左打印一行，行数固定为endRow ,列数从endColumn - 1逐渐减至start
            if (endColumn >= start + 1) {
                for (int i = endColumn - 1; i >= start; i--) {
                    result.add(matrix[endRow][i]);
                }
                //从下到上打印一列，列数固定为start，行数从endRow - 1 逐渐降至start + 1
                if (endRow >= start + 2) {
                    for (int i = endRow - 1; i >= start + 1; i--) {
                        result.add(matrix[i][start]);
                    }
                }
            }
        }
    }

    public void printMatrix(int[][] matrix) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return;
        }

        int rows = matrix.length;
        int columns = matrix[0].length;
        int start = 0;
        //循环打印一圈，打印的起点为(0,0),然后(1,1)
        while (rows > 2 * start && columns > 2 * start) {
            printCircle(matrix, rows, columns, start);
            start++;
        }
    }


    private void printCircle(int[][] matrix, int rows, int columns, int start) {
        //打印这一圈的最大行数
        int endRow = rows - 1 - start;
        //打印这一圈的最大列数
        int endColumn = columns - 1 - start;
        //从左到右打印一行
        for (int i = start; i <= endColumn; i++) {
            System.out.print(matrix[start][i]);
            System.out.print(" ");
        }
        //注意判断条件
        if (endRow > start) {
            //从上到下打印一行
            for (int i = start + 1; i <= endRow; i++) {
                System.out.print(matrix[i][endColumn]);
                System.out.print(" ");
            }
            //注意判断条件
            if (endColumn > start) {
                //从右到左打印一行
                for (int i = endColumn - 1; i >= start; i--) {
                    System.out.print(matrix[endRow][i]);
                    System.out.print(" ");
                }
                //注意判断条件
                if (endRow > start + 1){
                    //从下到上打印一行
                    for (int i = endRow - 1; i > start ; i--) {
                        System.out.print(matrix[i][start]);
                        System.out.print(" ");
                    }
                }
            }
        }
    }

}
